package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Siswa;
import edu.unindra.raport.dao.impl.SiswaImpl;

import javax.swing.JComboBox;

public class FormDetailMenuSiswaEdit extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JButton btnUbah = null;
	private JButton btnBatal = null;

	private FormDetailMenuSiswa parent;

	private JLabel lblNis;
	private JTextField txtNis;

	private JLabel lblTeTgl;
	private JLabel lblTaLahir;
	private JTextField txtTeTgl;
	private JTextField txtTaTgl;

	private SiswaImpl siImpl = null;

	private JLabel lblNama;
	private JTextField txtNama;
	@SuppressWarnings("rawtypes")
	private JComboBox cbKls;
	@SuppressWarnings("rawtypes")
	private JComboBox cbThnAjaran;
	private JLabel lblKls;
	private JLabel lblThnAjaran;

	public FormDetailMenuSiswaEdit(FormDetailMenuSiswa parent) {
		this.parent = parent;

		siImpl = new SiswaImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Ubah Data Siswa");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 276);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNis = new GridBagConstraints();
			gbc_lblNis.gridx = 1;
			gbc_lblNis.anchor = GridBagConstraints.WEST;
			gbc_lblNis.insets = new Insets(2, 2, 5, 5);
			gbc_lblNis.weightx = 0.0D;
			gbc_lblNis.gridy = 0;
			this.lblNis = new JLabel();
			lblNis.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNis.setText("Nis");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
					0.0, 0.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNis = new GridBagConstraints();
			gbc_txtNis.gridwidth = 3;
			gbc_txtNis.insets = new Insets(0, 0, 5, 5);
			gbc_txtNis.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNis.gridx = 3;
			gbc_txtNis.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNis);
			this.jContentPane.add(this.lblNis, gbc_lblNis);
			GridBagConstraints gbc_lblNama = new GridBagConstraints();
			gbc_lblNama.anchor = GridBagConstraints.WEST;
			gbc_lblNama.insets = new Insets(0, 0, 5, 5);
			gbc_lblNama.gridx = 1;
			gbc_lblNama.gridy = 1;
			jContentPane.add(getLblNama(), gbc_lblNama);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 1;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblTeTgl = new GridBagConstraints();
			gbc_lblTeTgl.anchor = GridBagConstraints.WEST;
			gbc_lblTeTgl.insets = new Insets(0, 0, 5, 5);
			gbc_lblTeTgl.gridx = 1;
			gbc_lblTeTgl.gridy = 2;
			jContentPane.add(getLblTeTgl(), gbc_lblTeTgl);
			GridBagConstraints gbc_txtTeTgl = new GridBagConstraints();
			gbc_txtTeTgl.gridwidth = 3;
			gbc_txtTeTgl.insets = new Insets(0, 0, 5, 5);
			gbc_txtTeTgl.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTeTgl.gridx = 3;
			gbc_txtTeTgl.gridy = 2;
			jContentPane.add(getTxtTeTgl(), gbc_txtTeTgl);
			GridBagConstraints gbc_lblTaLahir = new GridBagConstraints();
			gbc_lblTaLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTaLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTaLahir.gridx = 1;
			gbc_lblTaLahir.gridy = 3;
			jContentPane.add(getLblTaLahir(), gbc_lblTaLahir);
			GridBagConstraints gbc_txtTaTgl = new GridBagConstraints();
			gbc_txtTaTgl.gridwidth = 3;
			gbc_txtTaTgl.insets = new Insets(0, 0, 5, 5);
			gbc_txtTaTgl.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTaTgl.gridx = 3;
			gbc_txtTaTgl.gridy = 3;
			jContentPane.add(getTxtTaTgl(), gbc_txtTaTgl);
			GridBagConstraints gbc_lblKls = new GridBagConstraints();
			gbc_lblKls.anchor = GridBagConstraints.WEST;
			gbc_lblKls.insets = new Insets(0, 0, 5, 5);
			gbc_lblKls.gridx = 1;
			gbc_lblKls.gridy = 4;
			jContentPane.add(getLblKls(), gbc_lblKls);
			GridBagConstraints gbc_cbKls = new GridBagConstraints();
			gbc_cbKls.gridwidth = 3;
			gbc_cbKls.insets = new Insets(0, 0, 5, 5);
			gbc_cbKls.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbKls.gridx = 3;
			gbc_cbKls.gridy = 4;
			jContentPane.add(getCbKls(), gbc_cbKls);
			GridBagConstraints gbc_lblThnAjaran = new GridBagConstraints();
			gbc_lblThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_lblThnAjaran.gridx = 1;
			gbc_lblThnAjaran.gridy = 5;
			jContentPane.add(getLblThnAjaran(), gbc_lblThnAjaran);
			GridBagConstraints gbc_cbThnAjaran = new GridBagConstraints();
			gbc_cbThnAjaran.gridwidth = 3;
			gbc_cbThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_cbThnAjaran.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbThnAjaran.gridx = 3;
			gbc_cbThnAjaran.gridy = 5;
			jContentPane.add(getCbThnAjaran(), gbc_cbThnAjaran);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 6;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnUbah = new GridBagConstraints();
			gbc_btnUbah.gridx = 6;
			gbc_btnUbah.insets = new Insets(2, 2, 2, 0);
			gbc_btnUbah.anchor = 13;
			gbc_btnUbah.gridy = 6;
			this.jContentPane.add(getBtnUbah(), gbc_btnUbah);
		}
		return this.jContentPane;
	}

	private JButton getBtnUbah() {
		if (this.btnUbah == null) {
			this.btnUbah = new JButton();
			this.btnUbah.setText("Ubah");
			this.btnUbah.setToolTipText("Insert");
			this.btnUbah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int retVal = 0;
					Siswa si = new Siswa();

					si.setNis(txtNis.getText());
					si.setNama(txtNama.getText());
					si.setTeTgl(txtTeTgl.getText());
					si.setTaTgl(txtTaTgl.getText());
					si.setKls(Integer.valueOf(cbKls.getSelectedItem()
							.toString()));
					si.setThnAjaran(Integer.valueOf(cbThnAjaran
							.getSelectedItem().toString()));

					try {
						retVal = siImpl.update(si);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

					if (retVal > -1) {
						JOptionPane.showMessageDialog(
								FormDetailMenuSiswaEdit.this,
								"Proses update data siswa  berhasil");
						int i = parent.getTable().convertRowIndexToModel(
								parent.getTable().getSelectedRow());

						parent.getModelTable().setSiswaAt(si, i);

						FormDetailMenuSiswaEdit.this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuSiswaEdit.this,
								"Proses update data siswa  tidak berhasil");
					}
				}
			});
		}
		return this.btnUbah;
	}

	JTextField getTxtCardNumber() {
		if (txtNis == null) {
			txtNis = new JTextField();
			txtNis.setColumns(10);

			txtNis.setEnabled(false);
		}
		return txtNis;
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.setToolTipText("Insert");
			btnBatal.setText("Batal");
			btnBatal.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					FormDetailMenuSiswaEdit.this.setVisible(false);
				}
			});
		}
		return btnBatal;
	}

	private JLabel getLblTeTgl() {
		if (lblTeTgl == null) {
			lblTeTgl = new JLabel();
			lblTeTgl.setText("Tempat Lahir");
		}
		return lblTeTgl;
	}

	private JLabel getLblTaLahir() {
		if (lblTaLahir == null) {
			lblTaLahir = new JLabel();
			lblTaLahir.setText("Tanggal Lahir");
		}
		return lblTaLahir;
	}

	private JTextField getTxtTeTgl() {
		if (txtTeTgl == null) {
			txtTeTgl = new JTextField();
			txtTeTgl.setColumns(10);
		}
		return txtTeTgl;
	}

	private JTextField getTxtTaTgl() {
		if (txtTaTgl == null) {
			txtTaTgl = new JTextField();
			txtTaTgl.setEnabled(false);
			txtTaTgl.setColumns(10);
		}
		return txtTaTgl;
	}

	public void setSiswa(Siswa e) {
		if (e == null) {
			throw new IllegalArgumentException();
		}

		txtNis.setText(e.getNis());
		txtNama.setText(e.getNama());
		txtTeTgl.setText(e.getTaTgl());
		txtTaTgl.setText(e.getTeTgl());
		cbKls.getModel().setSelectedItem(e.getKls());
		cbThnAjaran.getModel().setSelectedItem(e.getThnAjaran());
	}

	private JLabel getLblNama() {
		if (lblNama == null) {
			lblNama = new JLabel();
			lblNama.setText("Nama Siswa");
			lblNama.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblNama;
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbKls() {
		if (cbKls == null) {
			cbKls = new JComboBox();
			cbKls.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
					"3", "4", "5", "6" }));
		}
		return cbKls;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbThnAjaran() {
		if (cbThnAjaran == null) {
			cbThnAjaran = new JComboBox();
			cbThnAjaran.setModel(new DefaultComboBoxModel(new String[] {
					"2005", "2006", "2007", "2008", "2009", "2010", "2011",
					"2012", "2013", "2014", "2015", "2016", "2017" }));
		}
		return cbThnAjaran;
	}

	private JLabel getLblKls() {
		if (lblKls == null) {
			lblKls = new JLabel();
			lblKls.setText("Kelas");
		}
		return lblKls;
	}

	private JLabel getLblThnAjaran() {
		if (lblThnAjaran == null) {
			lblThnAjaran = new JLabel();
			lblThnAjaran.setText("Tahun Ajaran");
		}
		return lblThnAjaran;
	}
}